package com.itheima.heima.info.manager.system.controller;

import com.itheima.heima.info.manager.system.domain.Teacher;
import com.itheima.heima.info.manager.system.service.TeacherService;

import java.util.Scanner;

/*
老师交互类
与老师 键盘录入  输出的代码
应该在这个类中
 */
public abstract class BaseTeacherController {
    //成员变量
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);

    //入口
    public final void start() {
        //编写老师的12345
        //所有的入口
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("新增老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用老师系统");
                    return;
                default:
                    System.out.println("请输入正确的选择");
            }
        }


    }

    //查
    public  final  void findAllTeacher() {
        Teacher[] teas = teacherService.findALl();
        if (teas == null) {
            System.out.println("没有数据,请先添加");
            return;
        }
        System.out.println("学号 姓名 年龄 生日");
        for (int i = 0; i < teas.length; i++) {
            Teacher tea = teas[i];
            if (tea != null) {
                System.out.println(tea.getSid() + "  " + tea.getName() + "  " + tea.getAge() + "  " + tea.getBirthday());
            }
        }
    }

    //改
    public  final void updateTeacher() {
        String sid = getTeacherSid();
        Teacher teacher = getTeacher(sid);
        teacherService.updateTeacher(sid, teacher);
    }



    public  final String getTeacherSid() {
        String sid = null;
        while (true) {
            System.out.println("请输入工号");
            sid = sc.next();
            boolean exist = teacherService.isExist(sid);
            if (exist) {
                break;
            } else {
                System.out.println("输入的学号不存在,请重新输入");
            }

        }
        return sid;
    }

    //删
    public  final  void deleteTeacher() {
        String sid = getTeacherSid();
        teacherService.deleteTeacher(sid);

    }

    //增
    public  final  void addTeacher() {
        String sid = null;
        while (true) {
            System.out.println("请输入工号");
            sid = sc.next();
            boolean exist = teacherService.isExist(sid);
            if (exist) {
                System.out.println("输入的学号已经存在,请重新输入");
            } else {
                break;
            }

        }
        Teacher teacher = getTeacher(sid);
        teacherService.addTeacher(teacher);
    }

    public abstract Teacher getTeacher(String sid);
}
